package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.CompanyService;
import redis.clients.jedis.Jedis;

public class CompanyServiceImpl implements CompanyService {
    @Override
    public ResultModel findAll(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel updateEnable(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel updateField(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel delete(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int update = companyDao.delete(companyModel);
        deleteRedisKey(update);
        return ResultModel.getModel(update);
    }

    @Override
    public ResultModel add(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int update = companyDao.delete(companyModel);
        deleteRedisKey(update);
        return ResultModel.getModel(update);
    }

    /**
     * 删除key值
     *
     * @param update
     */
    private void deleteRedisKey(Integer update) {
        //        一定是大于符号,
        if (update > 0) {
            Jedis jedis = new Jedis();
            jedis.del("logo");
        }
//        while (update<0){
//            update--;
//        }
    }

    @Override
    public ResultModel findById(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel update(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int update = companyDao.update(companyModel);
        deleteRedisKey(update);
        return ResultModel.getModel(update);
    }

    @Override
    public ResultModel getCount(CompanyModel companyModel) {
        return null;
    }

    /**
     * 1.查询redis是否有数据
     * 2.如果没有，则取数据库，并保存到redis
     * 3.如果有，则取redis并封装成需要的数据类型
     * 4.根据分析，我们的公司信息应该保存到String里面
     * -------------------------------------------
     *
     * @return
     */
    @Override
    public CompanyModel getFrontCompanyModel() {
        Jedis jedis = new Jedis();//取得redis对象
        Boolean key = jedis.exists("logo");
        CompanyModel companyModel = new CompanyModel();
        if (key) {
//            表示redis里面有数据，需要键所有数据取出来封装成CompanyModel
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setNet(jedis.get("net"));
            companyModel.setEmail(jedis.get("email"));
            companyModel.setLinePhone(jedis.get("linePhone"));
            companyModel.setAddress(jedis.get("address"));
            companyModel.setVersion(jedis.get("version"));
//            System.out.println("rediss");
        } else {
//            表示reids为空，则先查询数据库，然后保存到redis
            CompanyDao companyDao = new CompanyImpl();
            companyModel = companyDao.getFrontCompanyModel();
            jedis.set("logo", companyModel.getLogo());
            jedis.set("phone", companyModel.getPhone());
            jedis.set("email", companyModel.getEmail());
            jedis.set("net", companyModel.getNet());
            jedis.set("address", companyModel.getAddress());
            jedis.set("linePhone", companyModel.getLinePhone());
            jedis.set("version", companyModel.getVersion());
//            System.out.println("sql");
        }

        return companyModel;
    }
}
